﻿using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class GaiHuXXXX : System.Web.UI.Page
    {
        #region 属性

        #endregion

        #region 变量
        /// <summary>
        /// 来函信息BLL
        /// </summary>
        YS_LH_LAIHANXXProcess BllLaiHanXX = new YS_LH_LAIHANXXProcess();
        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL BllYongHuXX = new YS_YG_YONGHUXXBLL();
        /// <summary>
        /// 分户信息BLL
        /// </summary>
        YS_LH_FENHUXXBLL BllFenHuXX = new YS_LH_FENHUXXBLL();

        /// <summary>
        /// 来函用户信息
        /// </summary>
        YS_LH_YONGHUTZProcess BllLaiHanYHTZ = new YS_LH_YONGHUTZProcess();
        /// <summary>
        /// 原计划信息BLL
        /// </summary>
        YS_JH_YUANJIHUABLL BllYuanJH = new YS_JH_YUANJIHUABLL();
        /// <summary>
        /// 调整计划BLL
        /// </summary>
        YS_JH_TIAOZHENGJHBLL BllTiaoZhengJH = new YS_JH_TIAOZHENGJHBLL();

        YS_LH_GAIHUXXBLL BllGaiHuXX = new YS_LH_GAIHUXXBLL();
        #endregion

        #region 属性
        
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }
        #endregion

        #region 方法
        private void InitControl()
        {
           
            BindData();
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            string laihanbh = "";
            string yonghubh = "";
            int laihanid = 0;


            
            if (!int.TryParse(Request["id"], out laihanid))
            {
                return;
            }
            YS_LH_LAIHANXX laihanxx = BllLaiHanXX.SelectYS_LH_LAIHANXX(laihanid);

            if (laihanxx != null)
            {

                laihanbh = laihanxx.S_LAIHANBH;
                yonghubh = laihanxx.S_YONGHUBH;
            }

            BindValue(yonghubh);
            string where = string.Format(" S_LAIHANBH='{0}' ", laihanbh);

            DataTable lhable = BllLaiHanXX.SelectYS_LH_LAIHANXXesDynamic(where, " ID desc", null);
            if (lhable == null)
            {
                return;
            }
            string id = lhable.Rows[0]["ID"].ToString();
            YS_LH_LAIHANXX lh = BllLaiHanXX.SelectYS_LH_LAIHANXX(Convert.ToInt32(id));
            if (lh == null)
            {
                return;
            }
            UCLHUserInfo.LoadInfo(lh);
            
            txtLY.Text = lh.S_SHENQINGLY;



            DataTable userdt = BllYongHuXX.SelectYS_YG_YONGHUXXesDynamic(string.Format(" S_YONGHUH='{0}' and i_jlzt=0 and i_leixing=0", lh.S_YONGHUBH), "", null);
            
            where = " S_LAIHANBH='{0}' and  S_YUANYONGHuH='{1}' and  S_ZHANDIANBH='{2}' and S_GUANLIKBH='{3}'  and i_jlzt=0 ";

            where = string.Format(where, laihanxx.S_LAIHANBH, laihanxx.S_YONGHUBH, userdt.Rows[0]["S_ZHANDIANBH"], userdt.Rows[0]["S_GUANLIKBH"]);
            DataTable dt = BllLaiHanYHTZ.SelectYS_LH_YONGHUTZsDynamic(where, " ID  ", null);

            if (dt != null && dt.Rows.Count > 0)
            {
                txtYongHuHNEW.Text = dt.Rows[0]["S_YONGHUH"].ToString();
            }

            where = " S_LAIHANBH='{0}'  and i_jlzt=0 ";
            where = string.Format(where,lh.S_LAIHANBH);
            DataTable gaihu = BllGaiHuXX.SelectYS_LH_GAIHUXXesDynamic(where, "  i_liuchengbh", null);
            if (gaihu != null && gaihu.Rows.Count > 0)
            {
                foreach(DataRow dr in gaihu.Rows)
                {
                    int spjg = Convert.ToInt32(dr["i_shenpijg"].ToString() == "" ? "0" : dr["i_shenpijg"]);
                    eLaiHanLCBH lc = (eLaiHanLCBH)Convert.ToInt32(dr["I_LIUCHENGBH"]);
                    if (lc == eLaiHanLCBH.主任)
                    {
                        cZhuRen1.Checked = spjg == 1;
                        cZhuRen0.Checked = spjg == 0;
                        labZhuRen.Text = dr["S_SHENPIYJ"].ToString();
                    }
                    if (lc == eLaiHanLCBH.节水科)
                    {
                        cJieShui1.Checked = spjg == 1;
                        cJieShui0.Checked = spjg == 0;
                        labJieShui.Text = dr["S_SHENPIYJ"].ToString();
                    }
                    if (lc == eLaiHanLCBH.计财科)
                    {
                        CJiCai1.Checked = spjg == 1;
                        cJiCai0.Checked = spjg == 0;
                        labJiCai.Text = dr["S_SHENPIYJ"].ToString();
                    }
                    if (lc == eLaiHanLCBH.业务员)
                    {
                        txtYeWuY.Text = dr["S_SHENPIYJ"].ToString();
                    }
                }
            }
        }
        private void BindValue(string yonghuh)
        {
            string LaiHanYongHuH = yonghuh;
            int waterYM = BusinessTool.GetWaterYM();
            if (LaiHanYongHuH != null && LaiHanYongHuH.Length > 0)
            {                
                string y = waterYM.ToString().Substring(0, 4);

                string where = " S_YONGHUH='{0}' and I_Y={1}  ";
                where = string.Format(where, LaiHanYongHuH, y);

                DataTable dtY = BllYuanJH.SelectYS_JH_YUANJIHUAsDynamic(where, "", null);

                DataTable dtT = BllYuanJH.SelectYS_JH_YUANJIHUAsDynamic(where + " and I_TYPE=8 ", "", null);
                if (dtT != null && dtT.Rows.Count > 0)
                {
                    DataRow dr = dtT.Rows[0];
                    txtYuanJiH1.Text = dr["I_M1"].ToString();
                    txtYuanJiH2.Text = dr["I_M2"].ToString();
                    txtYuanJiH3.Text = dr["I_M3"].ToString();
                    txtYuanJiH4.Text = dr["I_M4"].ToString();
                    txtYuanJiH5.Text = dr["I_M5"].ToString();

                    txtYuanJiH6.Text = dr["I_M6"].ToString();
                    txtYuanJiH7.Text = dr["I_M7"].ToString();
                    txtYuanJiH8.Text = dr["I_M8"].ToString();
                    txtYuanJiH9.Text = dr["I_M9"].ToString();
                    txtYuanJiH10.Text = dr["I_M10"].ToString();

                    txtYuanJiH11.Text = dr["I_M11"].ToString();
                    txtYuanJiH12.Text = dr["I_M12"].ToString();
                    txtYuanJiH13.Text = (Convert.ToInt32(dr["I_M1"]) + Convert.ToInt32(dr["I_M2"]) + Convert.ToInt32(dr["I_M3"])
                        + Convert.ToInt32(dr["I_M4"]) + Convert.ToInt32(dr["I_M5"]) + Convert.ToInt32(dr["I_M6"])
                        + Convert.ToInt32(dr["I_M7"]) + Convert.ToInt32(dr["I_M8"]) + Convert.ToInt32(dr["I_M9"])
                        + Convert.ToInt32(dr["I_M10"]) + Convert.ToInt32(dr["I_M11"]) + Convert.ToInt32(dr["I_M12"])).ToString();

                }
                else if (dtY != null && dtY.Rows.Count > 0)
                {
                    DataRow dr = dtY.Rows[0];
                    txtYuanJiH1.Text = dr["I_M1"].ToString();
                    txtYuanJiH2.Text = dr["I_M2"].ToString();
                    txtYuanJiH3.Text = dr["I_M3"].ToString();
                    txtYuanJiH4.Text = dr["I_M4"].ToString();
                    txtYuanJiH5.Text = dr["I_M5"].ToString();

                    txtYuanJiH6.Text = dr["I_M6"].ToString();
                    txtYuanJiH7.Text = dr["I_M7"].ToString();
                    txtYuanJiH8.Text = dr["I_M8"].ToString();
                    txtYuanJiH9.Text = dr["I_M9"].ToString();
                    txtYuanJiH10.Text = dr["I_M10"].ToString();

                    txtYuanJiH11.Text = dr["I_M11"].ToString();
                    txtYuanJiH12.Text = dr["I_M12"].ToString();
                    txtYuanJiH13.Text = (Convert.ToInt32(dr["I_M1"]) + Convert.ToInt32(dr["I_M2"]) + Convert.ToInt32(dr["I_M3"])
                        + Convert.ToInt32(dr["I_M4"]) + Convert.ToInt32(dr["I_M5"]) + Convert.ToInt32(dr["I_M6"])
                        + Convert.ToInt32(dr["I_M7"]) + Convert.ToInt32(dr["I_M8"]) + Convert.ToInt32(dr["I_M9"])
                        + Convert.ToInt32(dr["I_M10"]) + Convert.ToInt32(dr["I_M11"]) + Convert.ToInt32(dr["I_M12"])).ToString();
                }
                else
                {
                    txtYuanJiH1.Text = "0";
                    txtYuanJiH2.Text = "0";
                    txtYuanJiH3.Text = "0";
                    txtYuanJiH4.Text = "0";
                    txtYuanJiH5.Text = "0";

                    txtYuanJiH6.Text = "0";
                    txtYuanJiH7.Text = "0";
                    txtYuanJiH8.Text = "0";
                    txtYuanJiH9.Text = "0";
                    txtYuanJiH10.Text = "0";

                    txtYuanJiH11.Text = "0";
                    txtYuanJiH12.Text = "0";
                    txtYuanJiH13.Text = "0";
                }
                DataTable dt = new YS_JH_YUANJIHUAProcess().SelectYS_JH_YUANJIHUAsDynamic("I_Y=" + waterYM / 100 + ",S_YONGHUH='" + yonghuh + "',AND I_JLZT=0", null);
                //int? syl_sy, yjh;
                //DataSet ds = new SpHelper().P_YS_LH_YONGHUYSSJCX(waterYM / 100, waterYM % 100, yonghuh, out yjh, out syl_sy);

                //DataSet ds = new SpHelper().P_YS_LH_YONGHUYSSJCX(Convert.ToInt32(y), null, LaiHanYongHuH, 1);
                //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                if(dt.Rows.Count>0)
                {
                    DataRow dr = dt.Rows[0];

                    txtShiYong1.Text = dr["IM1"].ToString();
                    txtShiYong2.Text = dr["IM2"].ToString();
                    txtShiYong3.Text = dr["IM3"].ToString();
                    txtShiYong4.Text = dr["IM4"].ToString();
                    txtShiYong5.Text = dr["IM5"].ToString();

                    txtShiYong6.Text = dr["IM6"].ToString();
                    txtShiYong7.Text = dr["IM7"].ToString();
                    txtShiYong8.Text = dr["IM8"].ToString();
                    txtShiYong9.Text = dr["IM9"].ToString();
                    txtShiYong10.Text = dr["IM10"].ToString();

                    txtShiYong11.Text = dr["IM11"].ToString();
                    txtShiYong12.Text = dr["IM12"].ToString();
                    txtShiYong13.Text = (Convert.ToInt32(dr["IM1"]) + Convert.ToInt32(dr["IM2"]) + Convert.ToInt32(dr["IM3"])
                        + Convert.ToInt32(dr["IM4"]) + Convert.ToInt32(dr["IM5"]) + Convert.ToInt32(dr["IM6"])
                        + Convert.ToInt32(dr["IM7"]) + Convert.ToInt32(dr["IM8"]) + Convert.ToInt32(dr["IM9"])
                        + Convert.ToInt32(dr["IM10"]) + Convert.ToInt32(dr["IM11"]) + Convert.ToInt32(dr["IM12"])).ToString();
                }
                txtXinJH1.Text = txtYuanJiH1.Text;
                txtXinJH2.Text = txtYuanJiH2.Text;
                txtXinJH3.Text = txtYuanJiH3.Text;
                txtXinJH4.Text = txtYuanJiH4.Text;
                txtXinJH5.Text = txtYuanJiH5.Text;

                txtXinJH6.Text = txtYuanJiH6.Text;
                txtXinJH7.Text = txtYuanJiH7.Text;
                txtXinJH8.Text = txtYuanJiH8.Text;
                txtXinJH9.Text = txtYuanJiH9.Text;
                txtXinJH10.Text = txtYuanJiH10.Text;

                txtXinJH11.Text = txtYuanJiH11.Text;
                txtXinJH12.Text = txtYuanJiH12.Text;
                txtXinJH13.Text = txtYuanJiH13.Text;



            }
        }
        #endregion
    }
}